/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "imageknifec/imageknifec.h"
#include "imageknife_wrapper.h"
#include "imageknife.h"

using namespace ImageKnifePro;

extern "C" {
ImageknifecError ImageknifecGetCacheImageByLoadSrc(const char *loadSrc,
                                                   ImageknifecGetCacheImageCallbackObject *onComplete,
                                                   ImageknifecCacheStrategy cacheTye, const char *signature)
{
    if (loadSrc == nullptr || onComplete == nullptr) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    auto option = std::make_shared<ImageKnifeOption>();
    option->loadSrc.SetString(loadSrc);
    if (signature != nullptr) {
        option->signature = signature;
    }

    auto callback = onComplete->callback;
    void *usrCtx = onComplete->usrCtx;
    auto funcInner = [callback, usrCtx](std::shared_ptr<ImageData> imageData) {
        if (callback == nullptr) {
            return;
        }

        if (imageData == nullptr) {
            callback(usrCtx, nullptr);
            return;
        }

        callback(usrCtx, reinterpret_cast<ImageknifecImageData *>(new ImageKnifeWrapper<ImageData>(imageData)));
    };
    ImageKnife::GetInstance().GetCacheImage(option, funcInner, static_cast<CacheStrategy>(cacheTye));
    return IMAGE_KNIFE_SUCCESS;
}
} // end of extern "C"